﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<script>
    var itemList = [];
    $(function () {
        $("#state_horizon a.btn-default").click(function () {
            $("#state_horizon a.btn-default").removeClass("active");
            $(this).addClass("active");
            $('#btn_search').trigger("click");
        });
        getItemList();
        getDistrict(0);
        getUserList();
        gridList(itemList);
        userinfo = getCache("DLXX");
        roleinfo = top.clients.role[userinfo.RoleId];
        //if (roleinfo.encode == "1002" || roleinfo.encode == "1003" || roleinfo.encode == "1007" || roleinfo.encode == "1008" || roleinfo.encode == "1018" || userinfo.F_Id =="95dbb33a-0d5c-11e8-bc3f-00163e00446b") {
        //    $("#xiaxian").show();
        //    $("#shenhezhong").show();
        //    $("#butongguo").show();
        //    $("#daishangxian").show();
        //    $("#NF-upExcel").show();
        //} else {
        //    $("#xiaxian").hide();
        //    $("#shenhezhong").hide();
        //    $("#butongguo").hide();
        //    $("#daishangxian").hide();
        //    $("#NF-upExcel").hide();
        //}
    })
    var userList;
    function getItemList() {
        itemList = getCache("SJZD");
        if (!itemList || itemList.length <= 0) {
            $.ajax({
                url: "/SiteManage/Site/GetItemListJson",
                dataType: "json",
                async: false,
                success: function (data) {
                    itemList = data;
                    initControl(data)
                    setCache("SJZD", itemList);
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }
        else {
            initControl(itemList);
        }
    }
    function getUserList() {
        $.ajax({
            url: "/SystemManage/User/GetUsersJson",
            dataType: "json",
            async: false,
            success: function (data) {
                userList = data;
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    }
    function gridList(ItemList) {
        var $gridList = $("#gridList");
        var queryJson = {
            keyword: $("#txt_keyword").val(),
            state: $("#state_horizon a.active").attr('data-value'),
        }
        $gridList.dataGrid({
            url: "/SiteManage/Site/GetGridJson",
            postData: { queryJson: JSON.stringify(queryJson) },
            height: $(window).height() - 195,
            colModel: [
                { label: '场地名称', name: 'title', width: 200, align: 'left' },
                {
                    label: '场地类型', name: 'site_type', width: 100, align: 'left',
                    formatter: function (cellvalue) {
                        return FMDictionaries(ItemList.SiteType, cellvalue);
                    }
                },
                {
                    label: '所属城市', name: 'city_id', width: 60, align: 'left',
                    formatter: function (cellvalue) {
                        return GetListByFId(cellvalue);
                    }
                },
                {
                    label: '行政区域', name: 'district_id', width: 60, align: 'left',
                    formatter: function (cellvalue) {
                        return GetListByFId(cellvalue);
                    }
                },
                { label: '地址', name: 'address', width: 200, align: 'left' },
                {
                    label: '场地级别', name: 'level', width: 120, align: 'center',
                    formatter: function (cellvalue) {
                        return FMDictionaries(ItemList.SiteLevel, cellvalue);
                    }
                },
                //{ label: '适合活动类型', name: 'site_event_type', width: 200, align: 'left' },
                {
                    label: '调性', name: 'site_style', width: 100, align: 'center',
                    formatter: function (cellvalue) {
                        return FMDictionaries(ItemList.site_style, cellvalue);
                    }
                },
                {
                    label: '状态', name: 'state', width: 60, align: 'center',
                    formatter: function (cellvalue) {
                        return FMDictionaries(ItemList.SiteState, cellvalue);
                    }
                },
                {
                    label: '最后更新时间', name: 'F_LastModifyTime', width: 150, align: 'left',
                    formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                },
                {
                    label: '最后更新人', name: 'F_LastModifyUserId', width: 120, align: 'left', formatter: function (cellvalue) {
                        if (userList != null && cellvalue != null) {
                            for (var i = 0; i < userList.length; i++) {
                                if (userList[i].F_Id == cellvalue) {
                                    cellvalue = userList[i].F_NickName;
                                    break;
                                } else {
                                    continue;
                                }
                            }
                        } else {
                            cellvalue = "";
                        }
                        return cellvalue;
                    }
                },
                { label: '', name: 'F_Id', hidden: true }
            ],
            pager: "#gridPager",    
            sortname: 'F_Id desc',
            viewrecords: true,
            multiselect: true,
            multiboxonly: true,
            ondblClickRow: function (rowid, iRow, iCol, e) {
                var rowobj = $gridList.getRowData(rowid); 
                $.addTab({
                    id: rowobj["F_Id"],
                    text: '修改场地',
                    href: "/SiteManage/Site/Form?keyValue=" + rowobj["F_Id"],
                    index: 10
                });
                
            }
        });


        $(".ui-jqgrid,.ui-jqgrid-view,.ui-jqgrid-htable,.ui-jqgrid-hdiv,.ui-jqgrid-bdiv,.ui-jqgrid-pager").css("width", function () { return "100%"; });
        $(".ui-jqgrid-btable").css("width", function () { return "99%"; });


        $("#btn_search").click(function () {
            if ($("#txt_min_area").val() != "" && $("#txt_max_area").val() != "" && $("#txt_min_area").val() > $("#txt_max_area").val()) {
                $.modalAlert("最小面积不能大于最大面积！", "warning");
                return false;
            }
            if ($("#txt_max_inner_price").val() != "" && $("#txt_min_inner_price").val()!="" && $("#txt_min_inner_price").val() > $("#txt_max_inner_price").val()) {
                $.modalAlert("最小单价不能大于最大单价！", "warning");
                return false;
            }
            var queryJsonSearch = {
                keyword: $("#txt_keyword").val(),
                state: $("#state_horizon a.active").attr('data-value'),
                site_type: $("#site_type").val(),
                provinces_id: $("#provinces_id").val(),
                city: $("#city_id").val(),
                district: $("#district_id").val(),
                min_area: $("#txt_min_area").val(),
                max_area: $("#txt_max_area").val(),
                min_inner_price: $("#txt_min_inner_price").val(),
                max_inner_price: $("#txt_max_inner_price").val(),
                height: $("#height").val(),
                max_seating_capacity: $("#txt_max_seating_capacity").val(),
                business_area: $("#business_area").val(),
                through_three_areas: $("#through_three_areas").val(),
            }
             $gridList.jqGrid('setGridParam', {
                 postData: { queryJson: JSON.stringify(queryJsonSearch) },
             }).trigger('reloadGrid');
         });
    }
    function FMDictionaries(state, cellvalue) {
        if (cellvalue != undefined && cellvalue != null) {
            for (var i = 0; i < state.length; i++) {
                if (state[i].F_ItemCode.toString() == cellvalue.toString()) {
                    cellvalue = state[i].F_ItemName;
                    break;
                }
            }
        }
        else
            cellvalue = "";
        return cellvalue;
    }
    function GetListByFId(cellvalue) {
        if (cellvalue != undefined && cellvalue != null) {
            $.ajax({
                url: "/SystemManage/Area/GetListByFId?code=" + cellvalue,
                dataType: "json",
                async: false,
                success: function (data) {
                    if (data.length > 0) {
                        cellvalue = data[0].F_FullName;
                    }
                }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        }
        else
            cellvalue = "";
        return cellvalue;
    }

    function getDistrict(type) {
        var pidValue, actionid;
        if (type == "city") {
            pidValue = $("#provinces_id").val();
            actionid = "city_id";
        }
        else if (type == 'district') {
            pidValue = $("#city_id").val()
            actionid = "district_id";
        }
        else if (type == 'business') {
            pidValue = $("#district_id").val();
            actionid = "business_area";
        }
        else {
            pidValue = 0;
            actionid = "provinces_id";
            $("#provinces_id").append("<option value='' class='select2-selection__placeholder'>==请选择省份==</option>");
        }
        $.ajax({
            url: "/SystemManage/Area/GetListByParentId?code=" + pidValue,
            dataType: "json",
            async: false,
            success: function (data) {
                CityControl(data, type, actionid);
            }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    }
    function CityControl(ItemList, type, actionid) {
        var items = ItemList;
        if (type == "district") {
            var bindid = $("#district_id,#business_area");
            bindid.empty();
            $("#district_id").append("<option value='' selected='selected'>==请选择行政区域==</option>");
            $("#business_area").append("<option value='' selected='selected'>==请选择商圈==</option>");
            bindid.bindSelect();
            bindid.select2();
        } else if (type == "city") {
            var bindid = $("#city_id,#district_id,#business_area");
            bindid.empty();
            $("#city_id").append("<option value='' selected='selected'>==请选择城市数据==</option>");
            $("#district_id").append("<option value='' selected='selected'>==请选择行政区域==</option>");
            $("#business_area").append("<option value='' selected='selected'>==请选择商圈==</option>");
            bindid.bindSelect();
            bindid.select2();
        }
        else if (type == "business") {
            var bindid = $("#business_area");
            bindid.empty();
            $("#business_area").append("<option value='' selected='selected'>==请选择商圈==</option>");
            bindid.bindSelect();
            bindid.select2();
        }
        for (var i = 0; i < items.length; i++) {
            $("#" + actionid).append("<option value='" + items[i].F_Id + "'>" + items[i].F_FullName + "</option>");
        }
        $("#" + actionid).bindSelect()//行政区域\商圈
        $("#" +actionid).select2();
    }
    function initControl(ItemList) {
        var sitetype = ItemList.SiteType;
        $("#site_type").append("<option value='' class='select2-selection__placeholder'>==请选择场地类型==</option>");
        for (var i = 0; i < sitetype.length; i++) {
            $("#site_type").append("<option value='" + sitetype[i].F_ItemCode + "'>" + sitetype[i].F_ItemName+ "</option>");
        }
        
        $("#city_id,#district_id,#site_type,#business_area,#through_three_areas").bindSelect()//行政区域
        $("#business_area").append("<option value='' class='select2-selection__placeholder'>==请选择商圈==</option>");
        $("#district_id").append("<option value='' class='select2-selection__placeholder'>==请选择行政区域==</option>");
        $("#city_id").append("<option value='' class='select2-selection__placeholder'>==请选择城市==</option>");
        $('#city_id,#district_id,#provinces_id,#site_type,#business_area,#through_three_areas').select2();
    }
    function btn_add() {
        $.modalOpen({
            id: "SiteForm",
            title: "新增场地",
            url: "/SiteManage/Site/Form?keyValue=SiteForm",
            width: "1200px",
            height: "650px",
            btn: null
        });
    }
    //附件管理
    function btn_attachment() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "AttachmentForm",
            title: "附件",
            url: "/SiteManage/Site/AttachmentForm?keyValue=" + keyValue,
            width: "700px",
            height: "620px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitForm();
                
            }
        });
    }
    function btn_photos() {
        var photoable_id = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "Photos",
            title: "图片管理",
            url: "/SiteManage/Photos/Photos?photoable_type=Site&photoable_id=" + photoable_id,
            width: "800px",
            height: "620px",
            btn: null
        });
    }

    function btn_edit() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        console.log("editSite:" + keyValue);
        if (keyValue) {
            $.addTab({
                id: keyValue,
                text: '修改场地',
                href: "/SiteManage/Site/Form?keyValue=" + keyValue,
                index: 10
            });
        } else {
            $.modalAlert("请先选择您要修改的场地数据！", "warning");
        }
    }
    function btn_delete() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        if (keyValue) {
            $.deleteForm({
                url: "/SiteManage/Site/DeleteForm",
                param: { keyValue: $("#gridList").jqGridRowValue().F_Id },
                success: function () {
                    $.currentWindow().$("#gridList").trigger("reloadGrid");
                }
            })
        } else {
            $.modalAlert("请先选择您要删除的场地数据！", "warning");
        }
    }

    function btn_details() {
        var keyValue = $("#gridList").jqGridRowValue().F_Id;
        $.modalOpen({
            id: "details",
            title: "场地详情",
            url: "/SiteManage/Site/Details?keyValue=" + keyValue,
            width: "1200px",
            height: "650px",
            btn: null,
        });
    }
    var arrList_FId = [];
    function btn_Excel() {
        arrList_FId = [];
        var rowIds = jQuery("#gridList").jqGrid('getGridParam', 'selarrrow');

        var queryJsonSearch = {
            keyword: $("#txt_keyword").val(),
            state: $("#state_horizon a.active").attr('data-value'),
            site_type: $("#site_type").val(),
            city: $("#city_id").val(),
            district: $("#district_id").val(),
            min_area: $("#txt_min_area").val(),
            max_area: $("#txt_max_area").val(),
            min_inner_price: $("#txt_min_inner_price").val(),
            max_inner_price: $("#txt_max_inner_price").val(),
            height: $("#height").val(),
            max_seating_capacity: $("#txt_max_seating_capacity").val(),
            business_area: $("#business_area").val(),
            through_three_areas: $("#through_three_areas").val(),
        }
        var opts = { btn: ['继续导出', '取消'] };
        $.modalConfirm("如果没有选择数据将导出所有筛选数据。确认要继续导出吗？", function (flag, index) {
            if (flag) {
                $.loading(true,"正在准备数据，请耐心等待！");
                for (var i = 0; i < rowIds.length; i++) {
                    //选中行的时间
                    var F_Id = $("#gridList").getCell(rowIds[i], "F_Id");
                    //建一个数组，把选中行的时间添加到这个数组中去。
                    arrList_FId.push(F_Id);
                }
                queryJsonSearch.keyvalues = arrList_FId.join(",").replace(/,/g, "','");
                $.ajax({
                    type: 'post',
                    url: "/SiteManage/Site/PostKey",
                    dataType: "json",
                    data: { queryJson: JSON.stringify(queryJsonSearch) },
                    async: false,
                    success: function (data) {
                        //console.log(data);
                        window.location.href = "/SiteManage/Site/ToExcel?key=" + data.message;
                    }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(XMLHttpRequest);
                        console.log(textStatus);
                        console.log(errorThrown);
                    }
                });

                $.loading(false);

                $.modalConfirmClose(index);
            }
        }, opts);
    }

</script>
<style type="text/css">
    .topPanel .search .input-group .form-control {
        padding: 4px 5px !important;
        height: 22px !important;
        margin: 0 10px !important;
        width: 80%;
    }
    .topPanel {
        height: 125px !important;
    }
    .topPanel .search table td {
        height:30px;
        line-height:30px;
    }
    .select2-container--default .select2-selection--single .select2-selection__rendered {
        color: #999 !important;
    }
    .checkbox, .radio {
        margin-top:10px !important;
    }
</style>

<div style="height:100%;">
    <div class="toolbar" style="width:25%;float:right; text-align:right;">
        <div class="btn-group">
            <a class="btn btn-primary" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span></a>
        </div>
        <div class="btn-group">
            <a id="NF-add" authorize="yes" class="btn btn-primary dropdown-text" onclick="btn_add()"><i class="fa fa-eraser"></i>添加场地</a>
        </div>
      
    </div>
    <div style="width:75%;">
        <table class="form">
            <tr>
                <td class="formValue" style="margin: 0 10px;">
                    <input id="txt_keyword" type="text" class="form-control" placeholder="关键字:场地名称、别名">
                </td>
                <td class="formValue">
                    <select id="provinces_id" onchange="getDistrict('city')" name="city_id" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="city_id" onchange="getDistrict('district')" name="city_id" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="district_id" onchange="getDistrict('business')" name="district_id" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="business_area" name="business_area" class="form-control"></select>
                </td>
            </tr>
            <tr>
                <td class="formValue">
                    <select id="site_type" name="site_type" class="form-control"></select>
                </td>
                <td class="formValue">
                    <select id="through_three_areas" name="through_three_areas" class="form-control required">
                        <option value="">==请选择落位区域==</option>
                        <option value="室内">室内</option>
                        <option value="室外">室外</option>
                        <option value="半开放式">半开放式</option>
                    </select>
                </td>
                <td class="formValue" style="margin: 0 10px;">
                    <input id="txt_min_area" style="width:48%;float:left;" type="text" class="form-control" placeholder="最小面积-">
                    <input id="txt_max_area" style="width:48%;float:right;" type="text" class="form-control" placeholder="+最大面积">
                </td>
                <td class="formValue" style="margin: 0 10px;">
                    <input id="txt_min_inner_price" style="width:48%;float:left;" type="text" class="form-control" placeholder="最小单价-">
                    <input id="txt_max_inner_price" style="width:48%;float:right;" type="text" class="form-control" placeholder="+最大单价">
                </td>
                <td class="formValue">
                    <input id="height" type="text" style="width:48%;float:left;" class="form-control" placeholder="层高" />
                    <input id="txt_max_seating_capacity" style="width:48%;float:right;" type="text" class="form-control" placeholder="最大容纳人数-" />
                </td>
            </tr>
            <tr>
                <td class="formValue" style="padding-left: 10px;" colspan="4">
                    <div id="state_horizon" class="btn-group">
                        <a class="btn btn-default active" data-value="">全部</a>
                        <a class="btn btn-default" data-value="1">上线</a>
                        <a class="btn btn-default" data-value="3">下线</a>
                        <a class="btn btn-default" id="jiamengdaishenhe" data-value="5">加盟待审核场地</a>
                    </div>
                </td>
                <td class="formValue" style="text-align:right">
                    <div class="input-group">
                        <button id="btn_search" type="button" class="btn  btn-primary" style="padding:0 20px">按条件筛选</button>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</div>

<div class="gridPanel">
    <table id="gridList"></table>
    <div id="gridPager"></div>
</div>


